import  redis
import urllib.request as ur
import time


class ProxyPool():
    def __init__(self):
        self.redis_con=redis.StrictRedis(
            host='localhost',
            port=6379,
            password='',
            decode_responses=True,
        )

    def set_proxy(self):
        #proxy=ur.urlopen('http://d.jghttp.golangapi.com/getip?num=5&type=1&pro=0&city=0&yys=0&port=1&pack=22669&ts=0&ys=0&cs=0&lb=4&sb=0&pb=4&mr=1&regions=').read().decode('utf-8').strip(' ')
        proxy_odd=None
        while True:
            proxy_new=ur.urlopen('http://d.jghttp.golangapi.com/getip?num=5&type=1&pro=0&city=0&yys=0&port=1&pack=22669&ts=0&ys=0&cs=0&lb=4&sb=0&pb=4&mr=1&regions=').read().decode('utf-8').strip(' ')
            if proxy_new!=proxy_odd:
                proxy_odd=proxy_new
                self.redis_conn.delete('proxy')
                self.redis_conn.sadd('proxy', *proxy_new)
                time.sleep(2)
            else:
                time.sleep(1)



    def get_proxy(self):
        proxy_s=self.redis_conn.srandmember('proxy',1)
        if proxy_s:
            return proxy_s[0]
        else:
            time.sleep(0.1)
            return self.get_proxy()

if __name__=='__main__':
    ProxyPool().get_proxy()